/*=========================================================================

    speak.cpp  -  Copyright (C) 1998-2005 by Don Cross

    This file contains a C++ function for speaking my chess moves,
    using a set of stock wave files with known filenames.
    This code works only in the Win32 environment.

=========================================================================*/

#include <assert.h>
#include <string.h>
#include <windows.h>
#include <mmsystem.h>
#include "chess.h"


static void PlayWav ( const char *prefix )
{
    char filename [256];
    strcpy ( filename, prefix );
    strcat ( filename, ".wav" );
    BOOL playResult = PlaySound ( filename, NULL, SND_FILENAME|SND_SYNC );
}


static void PlayWav ( char c )
{
    char prefix[2];
    prefix[0] = c;
    prefix[1] = '\0';
    PlayWav ( prefix );
}


void Speak (const char *pgn)
{
    int index = 0;

    if (pgn[0] == 'O') {
        // must be castling... either "O-O" or "O-O-O"
        if (pgn[1]=='-' && pgn[2]=='O') {
            if (pgn[3]=='-' && pgn[4]=='O') {
                PlayWav ("ooo");
                index = 5;	// skip over "O-O-O", in case there is a "+", etc.
            } else {
                PlayWav ("oo");
                index = 3;	// skip over "O-O", in case there is a "+", etc.
            }
        } else {
            assert (false);     // not valid PGN notation!
        }
    }

    char c;
    while ((c = pgn[index++]) != '\0') {
        switch (c) {
            case 'N':   PlayWav("knight");      break;
            case 'B':   PlayWav("bishop");      break;
            case 'R':   PlayWav("rook");        break;
            case 'Q':   PlayWav("queen");       break;
            case 'K':   PlayWav("king");        break;
            case 'x':   PlayWav("takes");       break;
            case '+':   PlayWav("check");       break;
            case '#':   PlayWav("mate");        break;
            case '=':   PlayWav("prom");        break;

            default:
                if ((c >= 'a' && c <= 'h') || (c >= '1' && c <= '8')) {
                    PlayWav (c);
                } else {
                    assert (false);     // unexpected character
                }
                break;
        }
    }
}
